package com.llmcu.msb.architect.concurrent.basic;

import java.util.concurrent.TimeUnit;

/**
 * interrupt() 无法打断 synchronized
 *
 * @author liuling
 * @date 2024/9/17 19:29
 */
public class InterruptAndSynchronized {
    static Object object = new Object();

    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
            synchronized (object) {
                System.out.println("t1 start...");
                try {
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("t1 end...");

            }
        }).start();
        TimeUnit.SECONDS.sleep(1);
        Thread t2 = new Thread(() -> {
            System.out.println("t2 palying...");
            synchronized (object) {
                System.out.println("t2 start...");
                System.out.println("t2 end...");
            }
        });
        t2.start();
        TimeUnit.SECONDS.sleep(1);
        t2.interrupt();
        System.out.println("主线程已执行t2打断操作，并执行完毕下班");

    }
}
